package zjc.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import zjc.dao.LiuyanDao;
import zjc.domain.Liuyan;

import java.util.List;

@Repository
public class LiuyanDaoImpl implements LiuyanDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void save(Liuyan liuyan) {
        String sql = "insert into liuyan(userId,title,lydate,details) values(?,?,?,?)";
        jdbcTemplate.update(sql,liuyan.getUserId(),liuyan.getTitle(),liuyan.getLydate(),liuyan.getDetails());
    }

    @Override
    public List<Liuyan> findAll() {
        String sql = "select * From liuyan";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Liuyan.class));

    }

    @Override
    public Liuyan findById(Long id) {
        String sql  = "select * from liuyan where id =?";
        return jdbcTemplate.queryForObject(sql,new Object[]{id},new BeanPropertyRowMapper<>(Liuyan.class));

    }

    @Override
    public void delete(Long id) {
        String sql = "delete from liuyan where id = ?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void update(Liuyan liuyan) {
        String sql = "update liuyan set userId = ?,title=?,details=?,lydate=? where id =?";
        jdbcTemplate.update(sql,liuyan.getUserId(),liuyan.getTitle(),liuyan.getDetails(),liuyan.getLydate(),liuyan.getId());
    }
}
